#include "game.h"

Sound *Menu::dzwiek;
int Menu::pos = 290;

bool Menu::initialize()
{ 
	// grafika menu
	if(!(Game::getResourceManager()->addResource("images/kula.png", Resource::ImageResource)))
		return false;
	if(!(Game::getResourceManager()->addResource("images/nowa_gra.png", Resource::ImageResource)))
		return false;
	if(!(Game::getResourceManager()->addResource("images/opcje.png", Resource::ImageResource)))
		return false;
	if(!(Game::getResourceManager()->addResource("images/rekordy.png", Resource::ImageResource)))
		return false;
	if(!(Game::getResourceManager()->addResource("images/koniec.png", Resource::ImageResource)))
		return false;
	if(!(Game::getResourceManager()->addResource("images/tlo.png", Resource::ImageResource)))
		return false;

	// dzwieki
	dzwiek = (Sound*)Game::getResourceManager()->addResource("sounds/menuitem.wav", Resource::SoundResource);
	if(!dzwiek)
		return false;

	// Inicjalizacja pomyslna
	return true;

}

void Menu::onKeyDown(BYTE keyCode)
{
	switch(keyCode)
	{
	case VK_DOWN:
		changePos(70);
		break;
	case VK_UP:
		changePos(-70);
		break;
	case VK_RETURN:
		runOption();
		break;
	}
}

void Menu::drawMenu()
{
	Image &kula = *(Image*)Game::getResourceManager()->getResourceByName("images/kula.png");
	Image &nowa_gra = *(Image*)Game::getResourceManager()->getResourceByName("images/nowa_gra.png");
	Image &opcje = *(Image*)Game::getResourceManager()->getResourceByName("images/opcje.png");
	Image &rekordy = *(Image*)Game::getResourceManager()->getResourceByName("images/rekordy.png");
	Image &koniec = *(Image*)Game::getResourceManager()->getResourceByName("images/koniec.png");
	Image &tlo = *(Image*)Game::getResourceManager()->getResourceByName("images/tlo.png");

	Game::getGraphics()->drawImage(tlo);
	Game::getGraphics()->drawImage(nowa_gra,250,290);
	Game::getGraphics()->drawImage(opcje,250,360);
	Game::getGraphics()->drawImage(rekordy,250,430);
	Game::getGraphics()->drawImage(koniec,250,500);
	Game::getGraphics()->drawImage(kula,180,pos);
}

void Menu::changePos(int x)
{
		Game::getSoundManager()->stopForeground(dzwiek);
			// renderuj 1 ramke
			pos += x;
			if(pos>500)
				pos = 290;
			if(pos<290)
				pos = 500;

		// drawMenu();
		
		Game::getSoundManager()->playForeground(dzwiek);

		// Sleep(200); /* krzys: wtf? */
}

void Menu::runOption()
{
	switch(pos)
	{
	case 290:
		nowa_gra();
		break;
	case 360:
		opcje();
		break;
	case 430:
		rekordy();
		break;
	case 500:
		koniec();
		break;
	}
}

void Menu::koniec()
{
		PostQuitMessage(0);
}

void Menu::nowa_gra()
{
	if(!Game::startNewGame())
	{
		// blad ladowania planszy!
	}
	else
	{
		// plansza zaladowana
		Game::setGameState(Game::GamePlayState);
	}
}

void Menu::opcje()
{

}

void Menu::rekordy()
{

}


